package fschmidt.util.java;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;


public final class FastFuture<V> implements Future<V> {
	private volatile V result;
	private volatile FutureTask<V> futureTask;

	public FastFuture(Callable<V> callable) {
		futureTask = new FutureTask<V>(callable);
	}

	public void run() {
		FutureTask<V> ft = futureTask;
		if( ft != null ) {
			ft.run();
		}
	}

	public boolean cancel(boolean mayInterruptIfRunning) {
		throw new UnsupportedOperationException();
	}

	public boolean isCancelled() {
		return false;
	}

	public boolean isDone() {
		return futureTask == null;
	}

	public V get() throws InterruptedException, ExecutionException {
		FutureTask<V> ft = futureTask;
		if( ft != null ) {
			result = ft.get();
			futureTask = null;
		}
		return result;
	}

	public V get(long timeout,TimeUnit unit) {
		throw new UnsupportedOperationException();
	}

}
